package com.equdao.car_violation_inquiry.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 此类用于工程中 公共常用方法
 * @author Administrator
 *
 */
public class CookieUtil {

	/***
	 * 设置cooke
	 * @param response
	 * @param name
	 * @param value
	 * @param maxAge
	 */
	public static void set(HttpServletResponse response,String name,String value,Integer maxAge){
		Cookie cookie = new Cookie(name,value);
		cookie.setPath("/");
		cookie.setMaxAge(maxAge);
		response.addCookie(cookie);
	}
	
	public static Cookie addCookie(HttpServletResponse response,String cookieName,String value,Integer maxAge,String domain) {  
        return addCookie(response, cookieName, value, maxAge, domain, "/");
    }
	
	public static Cookie addCookie(HttpServletResponse response,String cookieName,String value,Integer maxAge,String domain,String path) {  
        Cookie cookie = new Cookie(cookieName,value); 
        cookie.setMaxAge(maxAge);//maxAge cookie生命周期  以秒为单位 生存周期默认时间为秒，如果设置为负值的话，则为浏览器进程Cookie(内存中保存)，关闭浏览器就失效。
        if(null!=domain){
        	cookie.setDomain(domain);
        }
        cookie.setPath(path);
        response.addCookie(cookie);
        return cookie;  
    }
  
    // 获取 cookie  
    public static String getCookie(HttpServletRequest request, String cookieName) {  
        Cookie[] cookies = request.getCookies();  
        if (cookies != null) {  
            for (Cookie cookie : cookies) {  
                if (cookieName.equalsIgnoreCase(cookie.getName())) {  
                    return cookie.getValue();  
                }  
            }  
        }  
        return null;
    }
  
    // 删除 cookie  
    public static void delCookie(HttpServletResponse response, String cookieName) {
		Cookie cookie = new Cookie(cookieName,null);
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);
    }
	
	
	
	/**
	 * 从cookie中获取userId
	 * @param cookies
	 * @return
	 */
	public static Integer queryUserIdFromCookie(Cookie[] cookies){
		Integer userId = 0;
		// 然后迭代之  
		if (cookies != null && cookies.length > 0) { //如果没有设置过Cookie会返回null  
		    for (Cookie cookie : cookies) {
		    	if (cookie.getName().equals("userId")) {     
		    		userId = Integer.valueOf(cookie.getValue());   
		    		break;
		    	}  
		    }  
		}
		return userId;
	}
	
	/**
	 * 从cookie中获取登录名（登录名有可能是身份证号，邮箱和手机号）
	 * @param cookies
	 * @return
	 */
	public static String queryMobileFromCookie(Cookie[] cookies){
		String mobile = "";
		// 然后迭代之  
		if (cookies != null && cookies.length > 0) { //如果没有设置过Cookie会返回null  
		    for (Cookie cookie : cookies) {
		    	if (cookie.getName().equals("mobile")) {     
		    		mobile = String.valueOf(cookie.getValue());   
		    		break;
		    	}  
		    }  
		}
		return mobile;
	}
	
	/**
	 * 从cookie中获取uesrId 和登录名
	 * @param cookies
	 * @return
	 */
	public static Map<String,Object> queryUserIdAndMobileFromCookie(Cookie[] cookies){
		Integer userId = 0;
		String mobile = "";
		
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		// 然后迭代之  
		if (cookies != null && cookies.length > 0) { //如果没有设置过Cookie会返回null  
		    for (Cookie cookie : cookies) {
		    	//如果找到，跳出循环
		    	if(0!=userId){
		    		break;
		    	}
		  
		    	String name = cookie.getName();
		    	if (name.equals("userId")) {     
		    		userId = Integer.valueOf(cookie.getValue());   
		    	}else if(name.equals("mobile")){
		    		mobile = cookie.getValue();
		    	}
		    } 
		    rtnMap.put("userId", userId);
		    rtnMap.put("mobile", mobile);
		}
		return rtnMap;
	}
	
	/**
	 * 从cookies  中取出登录时生成的sessionId
	 */
	public static String querySessionIdFromCookie(Cookie[] cookies){
		
		String sessionId = "";
		
		if(cookies !=null&&cookies.length>0){
			for(Cookie cookie :cookies){
				if("equdaosessionId".equals(cookie.getName())){
					sessionId = cookie.getValue();
					break;
				}
			}
		}
		return sessionId;
	}
}
